<?php
namespace app\admins\controller;
use app\admins\model\Test as TestModel;

class Test extends Base
{
    // Curd复用
    use Curd;

    // 初始化
    protected function initialize()
    {
        $this->model = new TestModel();
        $this->sort = ['id' => 'desc'];
        $this->is_page = true;
        $this->rule = [
            'meta_key|键' => 'require|alphaDash|token|unique:test', // 验证token且值唯一
            'meta_value|值' => 'requireWith:meta_value|chsDash',
            'status|状态' => 'require|in:0,1',
        ];

        $action = $this->request->action();
        if ($action === 'save') {
            $this->param = $this->request->param();
            $this->param['time'] = time();
        }
    }

    // 数据返回
    private function datas($datas) {
        $datas = $datas->map(function($item){
            // $item['status'] = $item['status'] ? '<span style="color:#c2c2c2">禁用</span>' : '<span style="color:#5FB878">启用</span>';
            $item['time'] = date('Y-m-d H:i:s', $item['time']);
            return $item;
        });
        return $datas;
    }
}